package com.trp.view;

import android.content.Context;
import android.graphics.Canvas;

public class SegmentView extends GISView {

	private float[] mPoints;


	public SegmentView(Context context, double x1, double y1, double x2, double y2) {
		super(context);
		mPoints = new float[]{(float)x1, (float)y1, (float)x2, (float)y2};
		this.mViewType = GISView.Type.SEGMENT;
	}

	public SegmentView(Context context, float[] points) {
		super(context);
		if (points == null) {
			throw new NullPointerException(" points array can not be null");
		}
		mPoints = points;
		this.mViewType = GISView.Type.SEGMENT;
	}



	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		float[] f=new float[mPoints.length];
		System.arraycopy(mPoints, 0, f, 0, mPoints.length);
		Transformation.getInstance().mapWorldPointToScreen(f);
		canvas.drawLines(f, mStyle);
	}

	@Override
	protected boolean hitTest(int x, int y) {
		int count = this.mPoints.length / 2 - 1;
		for ( int i = 0; i < count; i+=2) {
			float startX = this.mPoints[i];
			float startY = this.mPoints[i+1];
			float k1 = (this.mPoints[i+3] - startY) / (this.mPoints[i+2] - startX);
			float k2 = (y - startY) / (x - startX);
			float d=Math.abs((k2-k1)/(1+k2*k1));
			//
			if (d<0.1) {
				return true;
			}
		}
		return false;
	}

	@Override
	protected void notifyLongPress() {

	}

	@Override
	protected void transform() {

	}

	@Override
	public void scaleChanged(float scale) {

	}



}
